//
//  MYIHomeHeaderView.m
//  MaYi
//
//  Created by 张志峰 on 2016/10/30.
//  Copyright © 2016年 zhifenx. All rights reserved.
//

#import "MHNearByHomeHeaderView.h"
#import "MHNearByHomeHeaderCollectionViewCell.h"
#import <CHTCollectionViewWaterfallLayout.h>
#import "MHNearyByHomeHeaderLayout.h"
#import "UIButton+ImageTitleSpacing.h"
#import "MHNearByModel.h"
//#import ""
static NSString *ID = @"homeCollectionViewCell";

@interface MHNearByHomeHeaderView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, strong) UICollectionView *collectionView;
/** 菜单按钮图片*/


@end

@implementation MHNearByHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

        [self setUpSubViews];
        
    
    }
    return self;
}

-(void)  setUpSubViews
{
    UILabel *nearByLable = [[UILabel alloc] init];
    [nearByLable setText:@"附近圈子"];
    [nearByLable setTextColor:MHTextColor333];
    [nearByLable setFont:MHRegularFont_13];
    nearByLable.frame = CGRectMake(12, 12, 100, 20 );
    [self addSubview:nearByLable];

    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(MH_SCREEN_WIDTH - 48 - 15, 11, 48, 24)];
    btn.titleLabel.font = [UIFont systemFontOfSize:12.f];
    [btn setTitle:@"更多" forState:UIControlStateNormal];
    [btn setTitleColor:MHMetionTextColor forState:UIControlStateNormal];
    [btn setTitleColor:MHMetionTextColor forState:UIControlStateSelected];
    [btn setImage:MHImageNamed(@"icon_found_more") forState:0];
    self.moreBtn = btn;
    [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
    [self addSubview:self.moreBtn];
    self.backgroundColor = MHWhiteTextColor;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.moreBtn.frame) , MH_SCREEN_WIDTH , 124) collectionViewLayout:[[MHNearyByHomeHeaderLayout alloc] init]];
    [collectionView registerClass:[MHNearByHomeHeaderCollectionViewCell class] forCellWithReuseIdentifier:ID];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(collectionView.frame), MH_SCREEN_WIDTH, 10)];
    view.backgroundColor = MH_MAIN_BACKGROUNDCOLOR_2;
    [self addSubview:view];
    
    UIView *lableContainer = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view.frame), MH_SCREEN_WIDTH, 44)];
    view.backgroundColor = MH_MAIN_BACKGROUNDCOLOR_2;
    [self addSubview:lableContainer];
    
    
    UILabel *nearByTopicLabel = [[UILabel alloc] init];
    [nearByTopicLabel setText:@"附近主题"];
    [nearByTopicLabel setTextColor:MHTextColor333];
    [nearByTopicLabel setFont:MHRegularFont_13];
    nearByTopicLabel.frame = CGRectMake(13, 13, 100, 20 );
    [lableContainer addSubview:nearByTopicLabel];
}

-(void)setDatas:(NSArray *)datas
{
    _datas = datas;
    [self.collectionView reloadData];
}

#pragma mark UICollectionViewDataSource 数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MHNearByHomeHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
     MHNearByModel *model =  self.datas[indexPath.row];
    [cell.iconView yy_setImageWithURL:[NSURL URLWithString:model.fullCoverUrl] placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt)];
    if(model.title.length > 5)
    {
        cell.title = [NSString stringWithFormat:@"%@...",[model.title substringWithRange:NSMakeRange(0, 5)]];
    }else
    {
        cell.title = model.title;
    }
    cell.distanceLabel.text = [NSString stringWithFormat:@"%.1fkm",model.distance < 0.1 ? 0.1 : model.distance];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
     MHNearByModel *model =  self.datas[indexPath.row];
    if (self.didselecBlock) {
        self.didselecBlock(model);
    }
}

@end
